<?php
// +----------------------------------------------------------------------
// | Polls(投票
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: PollsWidget.class.php 87 2011-04-03 10:52:52Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class PollsWidget extends Widget {

	/**
	 * 构造投票
	 * 
	 */
	public function render($data) {
	
		$vote = get_vote();
		
	    $output = '';
		$output .= '<!-- Polls: START -->'."\n";
		$output .= " <li class=\"widget widget_polls dbx-box\">\n";
		$output .= "  <h3 class=\"dbx-handle\">Polls</h3>\n";
		
		if (!empty($vote)) {
			$output .= "   <div id=\"polls\" class=\"wp-polls dbx-content\">\n";
			$output .= "    <form class=\"wp-polls-form\" method=\"post\" name=\"polls_form\">\n";
			$output .= "     <p style=\"display: none;\"><input name=\"poll_id\" value=\"".$vote['id']."\" type=\"hidden\"></p>\n";
			if (Cookie::get('voted_'.$vote['id'].'_'. C('COOKIEHASH'))) {
				$output .= "      <p style=\"text-align: center; padding-top:8px;\"><strong>".$vote['content'][$vote['id']]['vote_name']."</strong></p>\n";
				$output .= "       <div id=\"polls-ans\" class=\"wp-polls-ans\">\n";
				$output .= "        <ul class=\"wp-polls-ul\">\n";
				if ($vote['content'][$vote['id']]['vote_view']) {
					foreach ($vote['content'][$vote['id']]['options'] as $k=>$v) {
						$output .= "         <li>".$v['option_name']." <small>(".$v['percent']."%, ".$v['option_count']." Votes)</small>\n";
						$output .= "          <div class=\"pollbar\" style=\"width: ".$v['option_width']."%;\" title=\"".$v['option_name']." (".$v['percent']."% | ".$v['option_count']." Votes)\"></div>\n";
						$output .= "         </li>\n";
					}
				} else {
					foreach ($vote['content'][$vote['id']]['options'] as $k=>$v) {
						if ($vote['content'][$vote['id']]['vote_type']) {
							$output .= "         <li>\n";
							$output .= "          <input id=\"option_id_".$v['option_id']."\" name=\"option_id\" value=\"".$v['option_id']."\" type=\"checkbox\" onclick=\"NICJS.checkbox(this)\"/>\n";
							$output .= "          <label for=\"option_id_".$v['option_id']."\">";
							if ($v['option_url']) {
								$output .= "<a href=\"http://".$v['option_url']."\" rel=\"external nofollow\">".$v['option_name']."</a>";
							} else {
								$output .= $v['option_name'];
							}
							$output .= "</label>\n";
							$output .= "         </li>\n";
						} else {
							$output .= "         <li>\n";
							$output .= "          <input id=\"option_id_".$v['option_id']."\" name=\"option_id\" value=\"".$v['option_id']."\" type=\"radio\" onclick=\"NICJS.checkradio(this)\"/>\n";
							$output .= "          <label for=\"option_id_".$v['option_id']."\">";
							if ($v['option_url']) {
								$output .= "<a href=\"http://".$v['option_url']."\" rel=\"external nofollow\">".$v['option_name']."</a>";
							} else {
								$output .= $v['option_name'];
							}
							$output .= "</label>\n";
							$output .= "         </li>\n";	
						}
					}	
				}
				$output .= "        </ul>\n";
				$output .= "        <p style=\"text-align: center; font-size:7.5pt;\"><span style=\"color:#FF6600;\">Thanks for voting!</span> Total Voters: <strong>".$vote['content'][$vote['id']]['vote_count']."</strong>\n";
				$output .= "       </div>\n";			
			} else {
				$output .= "     <p style=\"display: none;\"><input name=\"type\" value=\"".$vote['content'][$vote['id']]['vote_type']."\" type=\"hidden\"></p>\n";
				$output .= "     <p style=\"display: none;\"><input name=\"limit\" value=\"".$vote['content'][$vote['id']]['vote_filterip']."\" type=\"hidden\"></p>\n";
				$output .= "     <p style=\"display: none;\"><input name=\"view\" value=\"".$vote['content'][$vote['id']]['vote_view']."\" type=\"hidden\"></p>\n";
				$output .= "     <p style=\"text-align: center; padding-top:8px;\"><strong>".$vote['content'][$vote['id']]['vote_name']."</strong></p>\n";
				$output .= "      <div id=\"polls-ans\" class=\"wp-polls-ans\">\n";
				$output .= "       <ul class=\"wp-polls-ul\">\n";
				foreach ($vote['content'][$vote['id']]['options'] as $k=>$v) {
					if ($vote['content'][$vote['id']]['vote_type']) {
						$output .= "        <li>\n";
						$output .= "         <input id=\"option_id_".$v['option_id']."\" name=\"option_id\" value=\"".$v['option_id']."\" type=\"checkbox\" onclick=\"NICJS.checkbox(this)\"/>\n";
						$output .= "         <label for=\"option_id_".$v['option_id']."\">";
						if ($v['option_url']) {
							$output .= "<a href=\"http://".$v['option_url']."\" rel=\"external nofollow\">".$v['option_name']."</a>";
						} else {
							$output .= $v['option_name'];
						}
						$output .= "</label>\n";
						$output .= "        </li>\n";
					} else {
						$output .= "        <li>\n";
						$output .= "         <input id=\"option_id_".$v['option_id']."\" name=\"option_id\" value=\"".$v['option_id']."\" type=\"radio\" onclick=\"NICJS.checkradio(this)\"/>\n";
						$output .= "         <label for=\"option_id_".$v['option_id']."\">";
						if ($v['option_url']) {
							$output .= "<a href=\"http://".$v['option_url']."\" rel=\"external nofollow\">".$v['option_name']."</a>";
						} else {
							$output .= $v['option_name'];
						}
						$output .= "</label>\n";
						$output .= "         </li>\n";	
					}
				}
				$output .= "        </ul>\n";
				$output .= "        <p style=\"text-align: center;\" id=\"voted\">\n";
				$output .= "         <input type=\"button\" value=\"   Vote   \" class=\"Buttons\" id=\"polls_submit\" disabled=\"disabled\"  onclick=\"NICJS.polls();\"/>\n";
				$output .= "        </p>\n";
				$output .= "       </div>\n";
			}
			$output .= "       </form>\n";
			$output .= "     </div>\n";
		} else {
			$output .=  "<p class=\"dbx-content\" style=\"text-align: center; font-size:11px;\">no polls available at the moment.</p>\n";
		}
		

		$output .= "   <div class=\"wp-polls-loading\"></div>\n";
		$output .= " </li>\n";
		$output .= ' <!-- Polls: END -->'."\n";
		
		return $output;
	}
	
}

?>